/*
 * Project: Code
 * File Created at 2024/1/13
 @Type Linked_list_cycle_ii.java
 @author Goodwin
 * Website :  https://leetcode.cn/problems/linked-list-cycle-ii/description/
*/

package Linked_List;

import Linked_List.pojo.ListNode;

public class Linked_list_cycle_ii {
    public ListNode detectCycle(ListNode head){
        if(head==null){
            return null;
        }
        ListNode slow = head, fast=head;
        while (fast!=null){
            slow = slow.next;

            if(fast.next !=null){
                fast = fast.next.next;
            }else {
                return null;
            }
            if(fast==slow){
                ListNode ptr = head;
                while (ptr!=slow){
                    ptr = ptr.next;
                    slow = slow.next;
                }
                return  ptr;
            }
        }
        return null;
    }
}
